package com.yww.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yww.entity.RoleModules;
import com.yww.entity.Roles;
import com.yww.service.IRolesService;
import com.yww.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2020-05-19
 */
@RestController
@RequestMapping("/roles")
public class RolesController {
    @Autowired
    IRolesService iRolesService;


    /**
     * (查)分页查询角色
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getRolesList")
    public Object getRolesList(Roles roles,@RequestParam(required = false,defaultValue = "1")Integer page,@RequestParam(required = false,defaultValue = "10")Integer limit){
        Page<Roles> p=new Page<>(page,limit);
        QueryWrapper<Roles> queryWrapper = new QueryWrapper<Roles>();
        if(!StringUtils.isEmpty(roles.getRoleName())) {
            queryWrapper.like("role_name", roles.getRoleName());
        }
        //查询方
        IPage<Roles> ipage = iRolesService.page(p, queryWrapper);
        //获取数据和总条数
        List<Roles> data = ipage.getRecords();
        long total = ipage.getTotal();
        return new Result(0,"",total,data);
    }

    /**
     * (查)根据用户id查询该用户角色们
     * @param userId
     * @return
     */
    @RequestMapping("/getRightRole")
    public Object getRightRole(Integer userId){
        List<Roles> list=iRolesService.getRightRole(userId);
        return new Result(0,"",list.size(),list);
    }


    /**
     * (增)添加角色
     * @param roles
     * @return
     */
    @RequestMapping("/addRoles")
    public Object addRoles(@Valid Roles roles){
        List<Roles> roleName = iRolesService.getRoleName(roles);
        if(roleName!=null && roleName.size()>0){
            return new Result(2,"该角色已存在");
        }else{
            boolean save = iRolesService.save(roles);
            if(save){
                return new Result(0,"添加成功");
            }
            return new Result(1,"添加失败");

        }

    }


    /**
     * (改)根据角色id修改角色
     * @param roles
     * @param roles
     * @return
     */
    @RequestMapping("/updRoles")
    public Object updRoles(@Valid Roles roles){
        List<Roles> roleName = iRolesService.getRoleName(roles);
        if(roleName!=null && roleName.size()>0){
            return new Result(2,"该角色已存在");
        }
        boolean b = iRolesService.updateById(roles);
        if(b){
            return new Result(0,"修改成功");
        }
            return new Result(1,"修改失败");
    }


    /**
     *(删)根据角色id删除角色
     * @param id
     * @return
     */
    @RequestMapping("/delRoles")
    public Object delRoles(@RequestParam Integer id){
        List<RoleModules> rolesContainsMod = iRolesService.getRolesContainsMod(id);
        if(rolesContainsMod!=null && rolesContainsMod.size()>0){
            return new Result(2,"该角色下有模块权限 不能删除");
        }
        boolean b = iRolesService.removeById(id);
        if(b){
            return new Result(0,"角色删除成功");
        }
            return new Result(1,"角色删除失败");
    }


    /**
     * (查)查询角色列表
     * @return
     */
    @RequestMapping("/getRolesAllList")
    public Object getRolesAllList(){
        List<Roles> list = iRolesService.list();
        return list;
    }

}
